﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using Motorola.Snapi;
using Motorola.Snapi.Constants;
using Motorola.Snapi.Constants.Enums;
using Motorola.Snapi.EventArguments;
using WeighSystem.Class;
//using WeighSystem.Interface;
namespace WeighSystem.Class
{

    class MotorZebraScannerNet
    {
        //https://github.com/Fortelinea/MotorolaCoreScannerNet/issues
        private const string ImageSaveLocation = @"C:\Users\Jason\Desktop\Image.jpg";

        private string _lastScanned = "";

        private bool _scannerAttached = false;

        public delegate void DataReceivedEventHandler(string e);
        public event DataReceivedEventHandler DataReceived;

        public MotorZebraScannerNet()
        {

        }
        /*
        public string GetDescription(this Enum value)
        {
            var fi = value.GetType().GetField(value.ToString());

            var attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);

            if ((attributes != null) && (attributes.Length > 0))
                return attributes[0].Description;
            return value.ToString();
        }*/
        public void BarcodeScannerInit()
        {
            try
            {
                FileLog.WriteLogToFile(FileLog.LOGLEVEL.信息, "打开BarcodeScannerManager 实例");
                BarcodeScannerManager.Instance.Open();
                FileLog.WriteLogToFile(FileLog.LOGLEVEL.信息, "注册事件扫码事件");
                BarcodeScannerManager.Instance.RegisterForEvents(EventType.Barcode, EventType.Pnp, EventType.Image, EventType.Other, EventType.Rmd);

                var b = BarcodeScannerManager.Instance.RegisteredEvents;

                //BarcodeScannerManager.Instance.Keyboard.EnableEmulation = false;

                BarcodeScannerManager.Instance.ScannerAttached += Instance_ScannerAttached;
                BarcodeScannerManager.Instance.ScannerDetached += Instance_ScannerDetached;

                var a = BarcodeScannerManager.Instance.DriverVersion;

                ConnectScanners();

                BarcodeScannerManager.Instance.DataReceived += Instance_DataReceived;
                BarcodeScannerManager.Instance.ImageReceived += Instance_ImageReceived;

                //Console.WriteLine("Ready to scan.. (Enter to quit)");
                //Console.ReadLine();
            }
            catch (Exception err)
            {

                FileLog.WriteLogToFile(FileLog.LOGLEVEL.信息, " BarcodeScannerInit出现异常:" + err.ToString());
            }
          
        }

        public void Instance_DataReceived(object sender, BarcodeScanEventArgs e)
        {
            _lastScanned = e.Data;
            var raw = Encoding.Default.GetString(e.RawData);
            //Console.WriteLine("Barcode type: " + e.BarcodeType.GetDescription());
            Console.WriteLine("Data: " + e.Data);
            FileLog.WriteLogToFile(FileLog.LOGLEVEL.信息, "收到扫码数据 :" + e.Data.ToString());
            if (DataReceived != null)
            DataReceived(e.Data);
        }

        public void Instance_ImageReceived(object sender, ImageEventArgs e)
        {
            e.Image.Save(ImageSaveLocation, e.Format);
            Console.WriteLine("Image saved to \"{0}\"", ImageSaveLocation);
            FileLog.WriteLogToFile(FileLog.LOGLEVEL.信息, "收到图片信息");
        }

        public void Instance_ScannerAttached(object sender, PnpEventArgs e)
        {
            FileLog.WriteLogToFile(FileLog.LOGLEVEL.信息, "发现扫码枪接入! id = " + e.ScannerId.ToString());
            _scannerAttached = true;
            ConnectScanners();
        }

        public void Instance_ScannerDetached(object sender, PnpEventArgs e)
        {
            FileLog.WriteLogToFile(FileLog.LOGLEVEL.信息, "发现扫码枪断开! id = " + e.ScannerId.ToString());
            _scannerAttached = false;
        }


        private void ConnectScanners()
        {
            foreach (var scanner in BarcodeScannerManager.Instance.GetDevices())
            {
                FileLog.WriteLogToFile(FileLog.LOGLEVEL.信息, "设置HostMode.USB_SNAPI_NoImaging");
                scanner.SetHostMode(HostMode.USB_CDC);
                if (scanner.Info.UsbHostMode != HostMode.USB_CDC)
                {
                    scanner.SetHostMode(HostMode.USB_CDC);
                    while (_scannerAttached == false)
                    {
                        Thread.Sleep(3000);
                    }
                }

                //scanner.Defaults.Restore();
                //scanner.CaptureMode = CaptureMode.Barcode;
                //GetAttributes(scanner);

                //PerformCommands(scanner);
                //scanner.Trigger.TriggerByCommand = true;
                //scanner.Trigger.PullTrigger();

                scanner.Actions.SetAttribute(138, 'B', 0); // sound beeper via attribute
            }
        }

        private void PerformCommands(IMotorolaBarcodeScanner scanner)
        {
            scanner.Actions.ToggleLed(LedMode.GreenOn);
            scanner.Actions.ToggleLed(LedMode.GreenOff);
            scanner.Actions.ToggleLed(LedMode.YellowOn);
            scanner.Actions.ToggleLed(LedMode.YellowOff);
            scanner.Actions.ToggleLed(LedMode.RedOn);
            scanner.Actions.ToggleLed(LedMode.RedOff);
            scanner.Actions.SoundBeeper(BeepPattern.FiveHighShort);
            scanner.Reboot();
        }



    }
}
